<?php
App::import('Model','User');

class Pharmacy extends User {
    var $useTable = 'users';
     var $validate = array();
     
     function ValidateData(){
        $this->validate = array(
                                "Username"=>array(
                                                    "rule1" => array(
                                                                    "rule" => "notEmpty",
                                                                    "message" => "Username can not empty",
                                                                    ),
                                                    "rule2" => array(
                                                                    "rule" => array('minLength', 4),
                                                                    "message" => "Usernames must be at least 4 characters long",
                                                                    ),
                                                    "rule3" => array(
                                                                    "rule" => array('maxLength', 50),
                                                                    "message" => "Usernames must be no larger than 10 characters long",
                                                                    ),
                                                    "rule4" => array(
                                                                    "rule" =>'checkUsernameExisted',
                                                                    "message" => "Username already exists",
                                                                    )
                                                ),
                                "FirstName" => array(
                                                    "rule1" => array(
                                                                        "rule" => "notEmpty",
                                                                        "message" => "FirstName can not empty",
                                                                    ),
                                                  
                                                ),
                                "LastName" => array(
                                                    "rule1" => array(
                                                                        "rule" => "notEmpty",
                                                                        "message" => "FirstName can not empty",
                                                                    )
                                                  
                                                ),
                                                
                                "Email" => array(
                                                    "rule1" => array(
                                                                        "rule" => "notEmpty",
                                                                        "message" => "Email can not empty",
                                                                    ),
                                                    "rule2" => array(
                                                                        "rule" => "email",
                                                                        "message" => "Email not avaliable !",
                                                                    ),
                                                     "rule3" => array(
                                                                    "rule" =>'checkEmailExisted',
                                                                    "message" => "Email already exists",
                                                                    )
                                                  
                                                ),
                            );
                            
       if($this->validates($this->validate)) 
        return TRUE; 
       else 
        return FALSE;  
    } 
    
    // Check exists username in database 
    function checkUsernameExisted(){
        if($this->data['Pharmacy']['Username']){ 
            // Check exists username in database
            if($this->data['Pharmacy']['id'])
                $user = $this->find('first',array('conditions'=>array('Pharmacy.Username'=>$this->data['Pharmacy']['Username'],'Pharmacy.UserType'=>2,'Pharmacy.id <>' =>$this->data['Pharmacy']['id'])));
            else
                $user = $this->find('first',array('conditions'=>array('Pharmacy.Username'=>$this->data['Pharmacy']['Username'],'Pharmacy.UserType'=>2)));
           // print_r($user);die;
            if(!empty($user))
                return false; // Already exist
            else 
                return true; // Not in the database
        }
        else{
            return false;
        }
    }
    
    
    // Check exists email in database 
    function checkEmailExisted(){
        if($this->data['Pharmacy']['Email']){ 
            // Check exists email in database
            if($this->data['Pharmacy']['id'])
                $email = $this->find('first',array('conditions'=>array('Pharmacy.Email'=>$this->data['Pharmacy']['Email'],'Pharmacy.UserType'=>2,'Pharmacy.id <>' =>$this->data['Pharmacy']['id'])));
            else
                $email = $this->find('first',array('conditions'=>array('Pharmacy.Email'=>$this->data['Pharmacy']['Email'],'Pharmacy.UserType'=>2)));

            if(!empty($email))
                return false; // Already exist
            else 
                return true; // Not in the database
        }
        else{
            return true;
        }
    }
    
	function getAccountByUsernameAndType($sz_UserName, $sz_UserType){
        
       return  $this->find('first',array( 'conditions'=>array(
                                                    'Pharmacy.Username'=>$sz_UserName,
       												'Pharmacy.UserType'=>$sz_UserType
                                                    ),
                                            )
        );  
    }
    
    
     function getListVacancyApplied($the_i_PharmacyId)
    {
         return $shopStation = $this->find('first',array('joins' => array(
                                                                          array(
                                                                              'table' => 'users',
                                                                              'alias' => 'User',
                                                                              'type' => 'inner',
                                                                              'conditions'=> array('Vacancy.Pharmacy_id = User.id')
                                                                              )),'fields'=>array('Vacancy.*,User.Username'),'conditions'=>array('Vacancy.id'=>$the_i_VacancyId,'Vacancy.Pharmacy_id'=>$the_i_PharmacyId)));
              
    }
}
?>